package com.zhihuishu.micro.course.openapi.app.impl.teacher;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.zhihuishu.micro.course.log.LoggerCollectionEnum;
import com.zhihuishu.micro.course.log.MDCLoggerUtil;
import com.zhihuishu.micro.course.model.MeetCourse;
import com.zhihuishu.micro.course.openapi.app.dto.MeetCourseOpenDto;
import com.zhihuishu.micro.course.openapi.app.teacher.AppMeetCourseOpenService;
import com.zhihuishu.micro.course.service.MeetCourseService;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;

/** 
 * @author yulijin 
 * @version 创建时间：2016年12月8日 下午1:14:28 
 * 类说明 
 */
@Service("appMeetCourseOpenService")
public class AppMeetCourseOpenServiceImpl implements AppMeetCourseOpenService {

	@Autowired
	private MeetCourseService meetCourseService;
	
	@Override
	public RemoteResult<List<MeetCourseOpenDto>> getMeetCourseList(Long courseId) throws RemoteException {
		
		RemoteResult<List<MeetCourseOpenDto>> result = new RemoteResult<List<MeetCourseOpenDto>>();
		
		try {
			List<MeetCourse> list = meetCourseService.getMeetCourseList(courseId);
			
			List<MeetCourseOpenDto> meetList = new ArrayList<MeetCourseOpenDto>();
			
			if (!CollectionUtils.isEmpty(list)){
				for(MeetCourse meetCourse : list){
					MeetCourseOpenDto meetDto = BeanHelper.transTo(meetCourse,MeetCourseOpenDto.class);
					meetDto.setSpeakerStr(meetCourse.getSpeakerName());
					meetList.add(meetDto);
				}
			}
			
			result.setResult(meetList);
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResult(null);
			result.setSuccess(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> getMeetCourse(Long meetCourseId) throws RemoteException {
		
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meetCourse = meetCourseService.getMeetCourse(meetCourseId);
			
			if (null != meetCourse) {
				MeetCourseOpenDto meetDto = BeanHelper.transTo(meetCourse,MeetCourseOpenDto.class);
				meetDto.setSpeakerStr(meetCourse.getSpeakerName());
				result.setResult(meetDto);
			} else {
				result.setResult(null);
			}
			
			result.setSuccess(true);
		} catch (Exception e) {
			result.setResult(null);
			result.setSuccess(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> insert(MeetCourseOpenDto meetCourse,Long updateUser) throws RemoteException {
		
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meet = BeanHelper.transTo(meetCourse,MeetCourse.class);
			meet.setSpeakerName(meetCourse.getSpeakerStr());
			meet.setIsAddScore(1);
			MeetCourse meetcourse = meetCourseService.insert(meet);
			result.setSuccess(true);
			result.setResult(BeanHelper.transTo(meetcourse,MeetCourseOpenDto.class));
			saveLogger("app添加见面课，本次详细信息：" + JSONObject.toJSONString(meetcourse),
					updateUser,meetCourse.getCourseId());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(null);
            String str = getStackTrace(e); 
			saveLogger("app添加见面课，本次报错详细信息：" + e.getMessage()+" || "+str,
					updateUser,meetCourse.getCourseId());
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<Boolean> remove(Long meetCourseId,Long updateUser,Long courseId) throws RemoteException {
		
		RemoteResult<Boolean> result = new RemoteResult<Boolean>();
		
		try {
			meetCourseService.remove(meetCourseId);
			result.setSuccess(true);
			result.setResult(true);
			saveLogger("app删除见面课，删除见面课id为：" + courseId,
					updateUser,courseId);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(false);
			result.setExceptionStack(e);
		}
		
		return result;
	}

	@Override
	public RemoteResult<MeetCourseOpenDto> update(MeetCourseOpenDto meetCourse,Long updateUser) throws RemoteException {
		RemoteResult<MeetCourseOpenDto> result = new RemoteResult<MeetCourseOpenDto>();
		
		try {
			MeetCourse meet = BeanHelper.transTo(meetCourse,MeetCourse.class);
			meet.setSpeakerName(meetCourse.getSpeakerStr());
			meetCourseService.update(meet);
			MeetCourseOpenDto meetCourseDto = BeanHelper.transTo(meetCourseService.getMeetCourse(meet.getMeetCourseId()),MeetCourseOpenDto.class);
			result.setSuccess(true);
			result.setResult(meetCourseDto);
			saveLogger("app修改见面课，本次详细信息：" + JSONObject.toJSONString(meetCourseDto),
					updateUser,meetCourse.getCourseId());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setResult(null);
			result.setExceptionStack(e);
		}
		
		return result;
	}
	
    // 添加日志
    private void saveLogger(String message, Long updateUser,Long courseId) {
        MDCLoggerUtil.saveLogger(message, updateUser, LoggerCollectionEnum.meetCourseCollection, courseId);
    }
	/**
	 * 获取异常堆栈信息
	 * @param t
	 * @return
	 */
	private String getStackTrace(Throwable t) {  
	    StringWriter sw = new StringWriter();  
	    PrintWriter pw = new PrintWriter(sw); 
	    try {  
	        t.printStackTrace(pw);  
	        return sw.toString();  
	    }  
	    finally {  
	        pw.close();  
	    }  
	} 
}
